﻿using System;

namespace OpenWaves.EPiServer.BrightcoveVideoCloudProperty
{
    public class QueryUrlBuilder
    {
        public static Uri Build(Query query)
        {
            var urlBuilder = new RelativeUrlBuilder();

            urlBuilder.Query.Add("page_size", query.ItemsPerPage);
            urlBuilder.Query.Add("page_number", query.PageNumber);

            urlBuilder.Query.Add("media_delivery", "default");
            urlBuilder.Query.Add("sort_by", "PUBLISH_DATE");
            urlBuilder.Query.Add("sort_order", "DESC");
            urlBuilder.Query.Add("get_item_count", "true");
            urlBuilder.Query.Add("video_fields", "name,thumbnailUrl,shortDescription,id,publishedDate");

            urlBuilder.Query.Add("token", VideoCloudService.Configuration.Access.ReadToken);

            if (string.IsNullOrWhiteSpace(query.Phrase))
            {
                urlBuilder.Query.Add("command", "find_all_videos");
            }
            else
            {
                urlBuilder.Query.Add("all", query.Phrase);
                urlBuilder.Query.Add("command", "search_videos");
            }

            return urlBuilder.ToUrl().Uri;
        }

        public static Uri Build(long videoId)
        {
            var urlBuilder = new RelativeUrlBuilder();

            urlBuilder.Query.Add("media_delivery", "default");
            urlBuilder.Query.Add("video_fields", "name,thumbnailUrl,id,publishedDate");
            urlBuilder.Query.Add("token", "X2tmupFJC9taZ_tZLEQkUju7QZkYf6qrcJU34QRdHhe25BCD5Nsa8Q..");

            urlBuilder.Query.Add("command", "find_video_by_id");
            urlBuilder.Query.Add("video_id", videoId);
            
            return urlBuilder.ToUrl().Uri;
        }
    }
}